define(["history"],function(_history) {
    function init(href,flag) {
        if(flag) {
            _history.push("shareEquipment", href);
        }
        var share = getModule();
        addEvent(share);
        $('#module-content').append(share);
    }

    function addEvent(module){
        module.find("#new-equipment").click(function() {
            create();
        });
        module.find(".equipment-item").click(function() {
           getOneShare();
        });
        module.find("#back").click(function() {
            module.find("#share-one").slideUp('slow',function() {
                module.find("#share-all").slideDown('slow');
            })
        });
    }

    function getModule() {
        var share;
        $.ajax({
            async : false,
            method : 'GET',
            url : 'app/lab/shareEquipment/shareEquipment.html',
            datType : 'string',
            success : function(response){
                if(typeof(response) === 'string'){
                    share = response;
                }
            }
        });
        return $(share);
    }

    function getData() {

    }

    function create() {
        var create = getCreate();
        create.appendTo("body");
        create.on('hidden.bs.modal', function (e) {
            create.remove();
        })
    }

    function getCreate() {
        var create;
        $.ajax({
            async : false,
            method : 'GET',
            url : 'app/lab/shareEquipment/new/new.html',
            datType : 'string',
            success : function(response){
                if(typeof(response) === 'string'){
                    create = response;
                }
            }
        });
        return $(create);
    }

    function getOneShare(shareID) {

        $("#share-all").slideUp('slow',function() {
            $("#share-one").slideDown('slow',function() {
                calendarInit($("#calendar"));
            });
        })

    }

    function calendarInit(div,url) {
        div.fullCalendar({
            // put your options and callbacks here
            header: {
                left:   'prev,next today',
                center: 'title',
                right:  'month,agendaWeek,agendaDay'
            },
            buttonText: {
                today:    '今天',
                month:    '月',
                week:     '周',
                day:      '日'
            },
            lang: 'zh-CN',
            selectable: true,
            selectHelper: true,
            select: function(start, end) {
                var title = prompt('Event Title:');
                var eventData;
                if (title) {
                    eventData = {
                        title: title,
                        start: start,
                        end: end
                    };
                    div.fullCalendar('renderEvent', eventData, true); // stick? = true
                }
                div.fullCalendar('unselect');
            },
            editable: true,
            dayClick: function(date, jsEvent, view) {

            },
            eventClick: function(calEvent, jsEvent, view) {
                alert(jsEvent);
            },
            events: {
                url: '../testAjax/get-events.php',
                error: function() {
                    $('#calendar_error').show();
                }
            }
            //color: 'yellow',    an option!
            //textColor: 'black' // an option!
        });
    }

    return {
        init : init
    }

});